package com.jomoo.account.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jomoo.account.service.AccountService;
import com.jomoo.common.pojo.JomooAccount;
import com.jomoo.common.tools.ExcelTool;
import com.jomoo.common.utils.BaseAction;

/**
 * jomoo平台账目控制层
 * 
 * @author 郝思聪
 * @date 2017年01月17日
 */
@Scope("prototype")
@Component("AccountAction")
public class AccountAction extends BaseAction
{

	private static final long serialVersionUID = -6607332233012917751L;

	Log log = LogFactory.getLog(AccountAction.class);

	@Resource
	AccountService accountService;

	/**
	 * 录入账目
	 * @return String
	 */
	@Action(value = "addAccount")
	public void saveAccount()
	{
		String strAccountType = request.getParameter("accountType");
		String strAccountNum = request.getParameter("accountnum");
		String strAccountTime = request.getParameter("accounttime");
		String strAccountRemark = request.getParameter("remark");
		
		Map<String, Boolean> map = new HashMap<String, Boolean>(10);
		Integer iType = null;
		Integer iCount = null;
		Float fNum = null;

		if (null != strAccountType && !"".equals(strAccountType))
		{
			iType = Integer.parseInt(strAccountType);
		}
		
		if (null != strAccountNum && !"".equals(strAccountNum))
		{
			fNum = Float.parseFloat(strAccountNum);
		}
		else
		{
			fNum = 0.0f;
		}

		try
		{
			// 传参录入信息
			iCount = accountService.addAccount(iType, fNum, strAccountTime, strAccountRemark);
			
			if(null != iCount)
			{
				map.put("result", true);
			}
			else
			{
				map.put("result", false);
			}

			// 直接用fastjson解析为json字符串输出
			returnData(JSON.toJSONString(map));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("查询产品价格异常：" + e.getMessage());
		}
	}
	
	/**
	 * 账单信息
	 */
	@Action(value = "getAccountInfo")
	public void getAccountInfo()
	{
		// 账单类型
		String strAccountType = request.getParameter("accounttype");
		
		// 结算状态
		String strAccountState = request.getParameter("accountstate");
		
		// 起始金额
		String strStartNum = request.getParameter("startnum");
		
		// 结束金额
		String strEndNum = request.getParameter("endtnum");
		
		// 起始查询时间
		String strStartTime = request.getParameter("begintime");
		
		// 结束查询时间
		String strEndTime = request.getParameter("endtime");
		
		/** 待转变量 **/
		Integer iType = null;
		Integer iState = null;
		Float fstartNum = null;
		Float fendNum = null;
		List<JomooAccount> accountList = null;

		if (null != strAccountType && !"".equals(strAccountType))
		{
			iType = Integer.parseInt(strAccountType);
		}
		if (null != strAccountState && !"".equals(strAccountState))
		{
			iState = Integer.parseInt(strAccountState);
		}
		if (null != strStartNum && !"".equals(strStartNum))
		{
			fstartNum = Float.parseFloat(strStartNum);
		}
		if (null != strEndNum && !"".equals(strEndNum))
		{
			fendNum = Float.parseFloat(strEndNum);
		}
		
		try
		{
			// 传参录入信息
			accountList = accountService.getServiceCards(iType, iState, 
														fstartNum, fendNum, strStartTime, strEndTime, null);
			
			// 直接用fastjson解析为json字符串输出
			returnData(JSON.toJSONString(accountList));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("查询账单信息异常：" + e.getMessage());
		}
	}
	
	/**
	 * 导出账单
	 */
	@SuppressWarnings("static-access")
	@Action(value = "getAccountExcle")
	public void getAccountExcle()
	{

		// 账单类型
		String strAccountType = request.getParameter("accounttype");
		
		// 结算状态
		String strAccountState = request.getParameter("accountstate");
		
		// 起始金额
		String strStartNum = request.getParameter("startnum");
		
		// 结束金额
		String strEndNum = request.getParameter("endtnum");
		
		// 起始查询时间
		String strStartTime = request.getParameter("begintime");
		
		// 结束查询时间
		String strEndTime = request.getParameter("endtime");
		
		/** 待转变量 **/
		Integer iType = null;
		Integer iState = null;
		Float fstartNum = null;
		Float fendNum = null;
		List<JomooAccount> accountList = null;

		if (null != strAccountType && !"".equals(strAccountType))
		{
			iType = Integer.parseInt(strAccountType);
		}
		if (null != strAccountState && !"".equals(strAccountState))
		{
			iState = Integer.parseInt(strAccountState);
		}
		if (null != strStartNum && !"".equals(strStartNum))
		{
			fstartNum = Float.parseFloat(strStartNum);
		}
		if (null != strEndNum && !"".equals(strEndNum))
		{
			fendNum = Float.parseFloat(strEndNum);
		}

		try
		{
			// 导出查询账单
			// 传参录入信息
			accountList = accountService.getServiceCards(iType, iState, 
														fstartNum, fendNum, strStartTime, strEndTime, "excle");
			
			ExcelTool.createExcel("平台财务信息报表", "阶段内财务汇总报表", accountList, log, "account");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("账单导出异常：" + e.getMessage());
		}
	}
	
	/**
	 * 作废账单信息
	 */
	@Action(value = "deleteAccountInfo")
	public void deleteAccountInfo()
	{
		String strAccountId = request.getParameter("id");
		
		Integer iCount = null;
		Integer iId = null;
		HashMap<String, Boolean> map = new HashMap<String, Boolean>(10);
		
		if(null != strAccountId && !"".equals(strAccountId))
		{
			iId = Integer.parseInt(strAccountId);
		}
		
		try
		{
			// 传参录入信息
			iCount = accountService.deleteAccount(iId);
			
			if(iCount > 0)
			{
				map.put("result", true);
			}
			else 
			{
				map.put("result", false);
			}
			
			// 直接用fastjson解析为json字符串输出
			returnData(JSON.toJSONString(map));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("查询账单信息异常：" + e.getMessage());
		}
	}
	
	@SuppressWarnings("rawtypes")
	@Action(value = "getMonthViewCount")
	public void getMonthViewCount()
	{
		List viewList = null;
		
		try
		{
			// 传参录入信息
			viewList = accountService.getMonthAccountView();
			
			// 直接用fastjson解析为json字符串输出
			returnData(JSON.toJSONString(viewList));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error("查询账单信息异常：" + e.getMessage());
		}
	}
	
	/**
	 * 图表查询
	 */
	@SuppressWarnings("rawtypes")
	@Action(value = "getAccountViewCreate")
	public void getAccountViewCreate()
	{
		// 生成图表类型
		String strGetType = request.getParameter("getType");
		
		// 客户
		String strCustomer = request.getParameter("customer");
		
		// 安装类型
		String strSetupType = request.getParameter("setuptype");
		
		// 产品分类
		String strClassiFy= request.getParameter("classify");
		
		// 安装状态
		String strSetupState = request.getParameter("setupstate");
		
		// 产品规格
		String strSpec = request.getParameter("spec");
		
		// 起始时间
		String strBeginTime = request.getParameter("begintime");
		
		// 终止时间
		String strEndTime = request.getParameter("endtime");
		
		// 转型声明
		Integer iType = null;
		Integer iCustomer = null;
		Integer iSetupType = null;
		Integer iProduct = null;
		Integer iSpec = null;
		Integer iSetupState = null;
		List accountViewList = null;
		
		if(null != strGetType && !"".equals(strGetType))
		{
			iType = Integer.parseInt(strGetType);
		}
		
		if(null != strCustomer && !"".equals(strCustomer))
		{
			iCustomer = Integer.parseInt(strCustomer);
		}
		
		if(null != strSetupType && !"".equals(strSetupType))
		{
			iSetupType = Integer.parseInt(strSetupType);
		}
		
		if(null != strClassiFy && !"".equals(strClassiFy))
		{
			iProduct = Integer.parseInt(strClassiFy);
		}
		
		if(null != strSpec && !"".equals(strSpec))
		{
			iSpec = Integer.parseInt(strSpec);
		}
		
		if(null != strSetupState && !"".equals(strSetupState))
		{
			iSetupState = Integer.parseInt(strSetupState);
		}
		
		try
		{
			// 执行查询并返回图表数据准备生成图表
			accountViewList = accountService.getAccountViewCreate(iType, iCustomer, iSetupType, iProduct, 
																iSpec, iSetupState, strBeginTime, strEndTime);
			
			returnData(JSON.toJSONString(accountViewList));
		}
		catch (Exception e)
		{
			log.error("生成账单类图表异常：" + e.getMessage());
		}
	}
}
